package com.wisemoresystem.seal.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.stereotype.Service;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_role;
import com.wisemoresystem.seal.util.SpringTools;

@Service
public class RoleService extends BaseService {

	public List<Map<String, Object>> selectRole() {
		String sql = "select * from t_role t where t.status=0  order by t.sort asc";

		List<Map<String, Object>> list = jt.queryForList(sql);
		List<Map<String, Object>> roles = new ArrayList<Map<String, Object>>();

		for (Map<String, Object> map : list) {
			String codes = (String) map.get("auth_codes");
			String[] code = codes.split(",");
			String value = "'0'";
			for (String string : code) {
				value += ",'" + string + "'";
			}
			String sql1 = "select group_concat(t.name) auth_codes from t_menu t where t.status=0 and t.code in("
					+ value + ") ";
			Map<String, Object> obj = jt.queryForMap(sql1);
			map.put("auth_codes", obj.get("auth_codes"));
			roles.add(map);
		}

		return roles;
	}

	public List<Map<String, Object>> selectRoleList(String bank_id) {
		String level = null;
		if(ChkTools.isNotNull(bank_id)){
			level = this.findObjById(bank_id, t_bank.class).getLevel_id();
		}
		if("6".equals(level)){
			level = "4";
		}
		String sql = "select * from t_role t"
				+ " where t.status=0 and t.auth_super like ? order by t.sort asc ";
		
		List<String> params = new ArrayList<String>();
		if(ChkTools.isNull(level)){
			sql = sql.replace("and t.auth_super like ?", "");
		}else{
			params.add("%"+level+"%");
		}
		List<Map<String, Object>> list = jt.queryForList(sql,params.toArray());
		return list;
	}

	public List<Map<String, Object>> selectLevelList() {
		String sql = "select * from t_bank_level t where t.status=0 order by t.sort asc ";
		List<Map<String, Object>> list = jt.queryForList(sql);
		return list;
	}

	public t_role selectRoleById(String role_id) {
		String hql = "from t_role t where t._id=? and t.status=0";
		t_role t = super.findObj(hql, role_id);
		return t;
	}

	public static void main(String[] args) {
		RoleService s = SpringTools.getBean(RoleService.class);
		System.err.println(s.selectRole());
	}
}
